package auxx;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.util.concurrent.TimeUnit;
import javax.swing.ImageIcon;

/**
 *
 * @author Yan Couto
 * @version 1.0
 */
public class Extra {
    protected Extra () { throw new AssertionError(); }
    
    public static String toOneString(String[] s,String divisor) {
        String aux = "";
        for(String a:s)
            aux+=a + divisor;
        return aux;
    }
    
    public static String[] toStringArray(Object[] array) {
        String[] a = new String[array.length];
        for(int i=0;i<a.length;i++)
            a[i] = array[i].toString();
        return a;
    }
    
    public static String encrypt(String s) {
        char[] c = s.toCharArray();
        for(int i=0;i<c.length;i++) {
            int cc = c[i];
            c[i] = (char) ((cc*2)-1);
        }
        return new String(c);
    }
    
    public static String decrypt(String s) {
        char[] c = s.toCharArray();
        for(int i=0;i<c.length;i++){
            int cc = c[i];
            cc = ++cc/2;
            c[i] = ((char) cc);
        }
        return new String(c);
    }
    
    public static void decryptAll(String[] s) {
        for(int i=0;i<s.length;i++)
            s[i] = decrypt(s[i]);
    }
    
    public static Integer[] toIntegerArray(int[] a) {
        Integer[] ar = new Integer[a.length];
        for(int i=0;i<a.length;i++)
            ar[i] = a[i];
        return ar;
    }
    
    public static int[] toIntArray(Integer[] a) {
        int[] ar = new int[a.length];
        for(int i=0;i<a.length;i++)
            ar[i] = a[i];
        return ar;
    }
    
    public static void wait(TimeUnit t,long time) {
        Object o = new Object();
        try {
            synchronized(o) {
                o.wait(t.toMillis(time));
            }
        } catch(Exception e) { e.printStackTrace(); }
    }
    
    public static int findClosing(String str,int pos) {
        char closing, opening = str.charAt(pos);
        switch(opening) {
            case '(':
                closing = (char) (opening + 1);
                break;
            case '[':
            case '<':
            case '{':
                closing = (char) (opening + 2);
                break;
            case '\'':
            case '\"':
                closing = opening;
                break;
            default:
                throw new IllegalArgumentException();
        }
        return str.indexOf(closing, pos+1);
    }
    
    public static Image flipHorizontally(Image i) {
        return new ImageIcon(i) {
            @Override  public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
                Graphics2D g2 = (Graphics2D) g;
                g2.translate(getIconWidth(),0);
                g2.scale(-1, 1);
                super.paintIcon(c, g2, x, y);
            }
        }.getImage();
    }
}
